package com.app.neteasecourse_aspectj.aspect;

import android.util.Log;

import com.app.neteasecourse_aspectj.anno.ClickButton;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

@Aspect
public class ClickAspect {

    @Pointcut("execution(void android.view.View.OnClickListener.onClick(..))")
    public void viewClickPointCut(){}
    long lastClickTime = 0L;
    @Around("viewClickPointCut()")
    public void dealViewClickPointCut(ProceedingJoinPoint joinPoint) throws Throwable {
        //做点击间隔判断
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastClickTime > 500) {
            lastClickTime = currentTime;
            Log.e("aspect>>>", "before view clicked");
            Object result = joinPoint.proceed();
            Log.e("aspect>>>", "after view clicked");
        } else {
            Log.e("aspect>>>", "两次点击时间间隔太短了，拒绝点击！");
        }

    }

    @Pointcut("execution(@com.app.neteasecourse_aspectj.anno.ClickButton * *(..))")
    public void clickButtonPointCut() {
        //声明点击Button的切点
    }

    @Around("clickButtonPointCut()")
    public Object dealClickButtonPointCut(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //方法名
        String className = methodSignature.getDeclaringType().getSimpleName();
        //类名
        String methodName = methodSignature.getName();
        //方法上的注解
        ClickButton annotation = methodSignature.getMethod().getAnnotation(ClickButton.class);
        String value = annotation.value();
        //在方法前do something
        Log.e("aspect>>>", "before: value=" + value);
        Object result = joinPoint.proceed();
        //在方法后do something
        Log.e("aspect>>>", "after: value=" + value);
        return result;
    }



}
